<?php
require_once 'data_form'.EXT;

class srs extends Data_form{

    function __construct(){
        parent::__construct();
        $this->load->helper('custom_string');
    }

    function index()
    {
        $this->load->model('srs_model');
        $data = array();
        $submit    =    ($this->input->post("submit") == "0" ? false : true);
        //load du lieu o trong db ra truoc
        $row    =    $this->srs_model->get_by('customer_id',$_SESSION["customer"]["id"]);
        if($_SERVER['REQUEST_METHOD'] == 'GET' && $row) 
        {
            //parse from row to array
            $data    =    array(
                "id" => $row->id,
                "customer_id" => $row->customer_id,
                "accessible_income_previous_year" => $row->accessible_income_previous_year,
                "explanation" =>     $row->explanation,
                "income_tax_paid" => $row->income_tax_paid,
                "with_srs" => $row->with_srs
                
            );
            
            $_SESSION["data"] = $data;
        }
        else if($_SERVER['REQUEST_METHOD'] == 'POST')
        {
        	$data["customer_id"]  =    $_SESSION["customer"]["id"];
            $data["accessible_income_previous_year"]           =    $_POST["textfield5"];
            $data["income_tax_paid"]          =    $_POST["hpaid"];
                //$data["explanation"] = $_POST["textfield3"]; 
            $data["with_srs"]           =    $_POST["textfield6"];
            if (isset($_POST['btnCalculate']) && $_POST['btnCalculate'] == 'Calculate')
            {
                if(isset($_SESSION["data"]['explanation'])) $data['explanation']	=	$_SESSION['data']['explanation'];
                $_SESSION["data"] = $data; 
            }
            else
            {
                 //$data = $_SESSION["data"];
                 $data["explanation"] = $_POST["textfield3"];    
                 //neu du lieu da co trong db
                if($row){
                    //update du lieu dang co trong db
                    $this->srs_model->update($row->id,$data);
                }
                else
                {
                    //tao moi du lieu trong db
                    $this->srs_model->insert($data);
                }
                //echo "<script>window.close();</script>";
                redirect(current_link());
            }       
        }
        
        //gan du lieu o trong row vao 1 bien trong data
        //truyen ra view thong qua data['row'] 
        $this->data['row']    =    $data;  
        $this->load->view("srs/index", $this->data);
    }
    
    function get_paid()
    {
    	$x	=	$this->input->post('value');
    	if($x <= 20000){
        	$y = 0;
        }
        elseif($x > 20000 && $x <= 30000){
        	$y = 3.5;
        }
        elseif($x > 30000 && $x <= 40000){
        	$y = 5.5;
        }
        elseif($x > 40000 && $x <= 80000){
        	$y = 8.5;
        }
        elseif($x > 80000 && $x <= 160000){
        	$y = 14;
        }
        elseif($x > 160000 && $x <= 320000){
        	$y = 17;
        }
        elseif($x > 320000){
        	$y = 20;
        }
        die ("".$y);
    }
}